﻿using RayDen.Library.Components.Color.Types;
using RayDen.Library.Core.Primitives;
using RayDen.Library.Data;
using RayDen.RayEngine.Core.Interface;
using RayDen.RayEngine.Corrector.Managers;
using RayEngine.Spectral.Entities.Textures;

namespace RayEngine.Spectral.Entities.Materials
{
    public class LightMaterial : BaseMaterial
    {
        private readonly IValueTexture emissionTexture; // consider option with iwavelengthdependent

        public LightMaterial(EmissionProfile profile)
        {
            if (profile.EmissionTexture != null)
            {
                emissionTexture =
                    TextureFactory.Instance.CreateImageTexture((RgbSpectrumTexture) profile.EmissionTexture);
            }
            else
            {
                var color = ColorManager.Instance.Convert(profile.EmissionSpectra, SpectrumType.Illuminant);
                emissionTexture = TextureFactory.Instance.CreateConstTexture(color);
            }            
        }

        public LightMaterial(SampledSpectrum spd)
        {
            var color = ColorManager.Instance.Convert(spd, SpectrumType.Illuminant);
            emissionTexture = TextureFactory.Instance.CreateConstTexture(color);
        }

        public LightMaterial(IValueTexture tex)
        {
            this.emissionTexture = tex;
        }

        public IColorType GetEmittance(float u, float v, out float pdf)
        {
            pdf = 1f;
            return (emissionTexture.Evaluate(u, v));
        }
    }
}